/* 需求广场 */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table, DatePicker, Modal, Select } from 'antd'
/* 引用接口文档路径 */
import {
    demandSquareSelect,demandSquareHide
} from 'actions/companyManage'
import {getbasetypefetch} from 'actions/common'
import ShowBigImg from '../../components/showBigImg'
import '../../style/userControl.less'



const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      demandTypeList:[],
      showBigImgModal:false,//查看大图的弹窗
      imgArry:[],//图片数组
      imgIndex:"",//图片下标
    }
    this.keyword={"demandTitle":"","demandType":""};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
  }
  componentDidMount() {
    this.selectList();
    this.getAuditStatus();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "demandTitle":this.props.form.getFieldValue('demandTitle'),
      "demandType":this.props.form.getFieldValue('demandType'),
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(demandSquareSelect(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }

  showBigImg(item,index){//点击查看大图
    //item 图片数组 index 点击的下标
    this.setState({
      showBigImgModal:true,
      imgArry:item,
      imgIndex:index
    })
  }
  showBigImgModalCancel(){//关闭查看大图弹窗
    this.setState({
      showBigImgModal:false,
      imgArry:[],
      imgIndex:''
    })
  }
  
  getAuditStatus(){
    this.props.dispatch(getbasetypefetch(
      {"type":16},
      (respose) => {
        this.setState({
          demandTypeList: respose.respData.resultData,
        })
    }))
  }
  hideBtn(id){
    //需求的显示隐藏
    Modal.confirm({
      content:"确定要执行改操作么？",
      onOk:()=>{
        this.props.dispatch(demandSquareHide(
          {"demandId":id},
          (respose) => {
            this.selectList();
        }))
      },
      onCancel:()=>{

      }
    })
  }


  columns() {//表格标题
    return [
      {
        title: '需求编号',
        key: 'demandCode',
        dataIndex: 'demandCode',
        width: '10%',
      },
      {
        title: '需求标题',
        dataIndex: 'demandTitle',
        key: 'demandTitle',
        width: '10%',
      },
      {
        title: '需求类型',
        dataIndex: 'demandTypeName',
        key: 'demandTypeName',
        width: '10%',
      },
      {
        title: '所属人',
        dataIndex: 'demandName',
        key: 'demandName',
        width: '10%',
      },
      {
        title: '需求状态',
        dataIndex: 'demandStatus',
        key: 'demandStatus',
        width: '10%',
        render: (text, recordId, index) => 
        <span>{text==1?'进行中':(text==2?'已解决':'已取消')}</span>,
      },
      {
        title: '显示状态',
        dataIndex: 'hideStatus',
        key: 'hideStatus',
        width: '10%',
        render: (text, recordId, index) => 
        <span>{text==0?'显示':'隐藏'}</span>,
      },
      {
        title: '需求图片',
        dataIndex: 'imagesURL',
        key: 'imagesURL',
        width: '15%',
        render: (text, recordId, index) => 
        <div>
          {
            text.map((item,i)=> {
              return(
                <div key={i} style={{cursor:'pointer'}} className="imgDiv" onClick={this.showBigImg.bind(this,text,i)}>
                  <img src={item} title="点击查看大图"/>                  
                </div>
              )
            })
          }
        </div>,
      },
      {
        title: '需求说明',
        dataIndex: 'demandExplain',
        key: 'demandExplain',
        width: '15%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          <Button key="seeinfo" type="primary" size="small" onClick={this.hideBtn.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>{text.hideStatus==0?'隐藏':'显示'}</Button>   
        </div>,
      },
      
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const respData = this.state.list.respData ? this.state.list.respData : [];//刷新页面调用接口数据赋值
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="需求标题">
              {
                getFieldDecorator('demandTitle', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <FormItem label="需求类型">
              {
                getFieldDecorator('demandType', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Select placeholder="请选择需求类型"    style={{ width: 200 }} >
                    <Select.Option value={""} key={98}>全部</Select.Option>                  
                    {
                      this.state.demandTypeList.map((item,i)=> {
                        return(
                          <Select.Option value={item.code} key={i}>{item.value}</Select.Option>
                        )
                      })
                    }
                  </Select>
                )
              }  
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={respData.resultData}
            pagination={{ total: respData.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>
        {/* 查看大图的弹窗 */}
        {
          this.state.showBigImgModal?
          <ShowBigImg visible={this.state.showBigImgModal} imgArry={this.state.imgArry} imgIndex={this.state.imgIndex} closeModal={this.showBigImgModalCancel.bind(this)} />
          :""
        }
      </div>
    )
  }
}
